package toubiao.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;











import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import toubiao.dao.business.ContactDaoI;
import toubiao.dao.business.DepartmentDaoI;
import toubiao.dao.business.DesignerDaoI;
import toubiao.dao.business.PartADaoI;
import toubiao.domain.po.business.PoContact;
import toubiao.domain.po.business.PoDepartment;
import toubiao.domain.po.business.PoDesigner;
import toubiao.domain.po.business.PoPartA;
import toubiao.utils.ProUtilLogger;
import utils.UtilBean;
import utils.UtilString;
import utils.easyui.UtilEzTree;
import utils.easyui.domain.EzTree;

@Service
public class TreeServiceImpl {

	@Autowired
	private DepartmentDaoI departmentDao;
	
	@Autowired
	private DesignerDaoI designerDao;
	
	@Autowired
	private PartADaoI partADao;
	
	@Autowired
	private ContactDaoI contactDao;
	
	
	public List<EzTree> getLtrdTreeOfDepartmentWithDesigner(String idsChecked){
		List<Long> idList=new ArrayList<Long>(0);
		if(idsChecked!=null&& !idsChecked.equals("")){
			String[] idArray=idsChecked.split(",");
			for(String id:idArray){
				Long idLong=Long.parseLong(id);
				idList.add(idLong);
			}
		}
		List<EzTree> treeList=new ArrayList<>();
		List<PoDepartment> departmentList=departmentDao.getAll(PoDepartment.class);
		for(PoDepartment d:departmentList){
			EzTree tree=new EzTree();
			tree.setId("department_"+d.getId());
			tree.setText(d.getName());
			tree.setState("closed");
			//tree.setIconCls("icon-edit");
			Map<String, Object> attr = tree.getAttributes();
			attr.put("id", d.getId());
			attr.put("name", d.getName());
			attr.put("isLeaf", false);
			treeList.add(tree);
			for(PoDesigner designer:d.getDesignerSet()){
				EzTree t=new EzTree();
				t.setId(""+designer.getId());
				t.setText(designer.getName());
				t.setPid("department_"+d.getId());
				t.setState("closed");
				if(idList.contains(designer.getId())){
					t.setChecked(true);
				}
				Map<String, Object> attr2 = t.getAttributes();
				attr2.put("id", designer.getId());
				attr2.put("name", designer.getName());
				attr2.put("departmentName", d.getName());
				attr2.put("phone", designer.getPhone());
				attr2.put("isLeaf", true);
				treeList.add(t);
			}
		}
		UtilEzTree.setTreeLeafStateOpen(treeList);
		UtilEzTree.setTreeCheckedByIds(treeList, idsChecked);
		return treeList;
	}
	
	public List<EzTree> getLtrdTreeOfPartA(String idsChecked){
		List<Long> idList=new ArrayList<Long>(0);
		if(idsChecked!=null&& !idsChecked.equals("")){
			String[] idArray=idsChecked.split(",");
			for(String id:idArray){
				Long idLong=Long.parseLong(id);
				idList.add(idLong);
			}
		}
		
		List<EzTree> treeList=new ArrayList<>();
		String[] typeArray={"联通","铁塔","移动","电线","企业","政府"};
		//String hql="select p from PoPartA where p.type in (:typeArray)";
		
	/*	Map<String,Object> paraMap=new HashMap<String, Object>();
		paraMap.put("typeArray", typeArray);
		List<PoPartA> partAList=partADao.find(hql, paraMap);*/
		
		//create type node
		
		List<EzTree> treeListOfType=new ArrayList<>();
		for(String type:typeArray){
			EzTree typeNode=new EzTree();
			typeNode.setId(type);
			typeNode.setText(type);
			treeListOfType.add(typeNode);
		}
		
		//create province node
		List<EzTree> treeListOfProvince=new ArrayList<>();
		String hqlOfProince="select p.type,p.province from PoPartA p group by p.type,p.province order by p.province";
		List<Object[]> proinceNameList=partADao.getObjectArrayByHql(hqlOfProince);
		for(Object[] proince:proinceNameList){
			EzTree ezTree=new EzTree();
			ezTree.setId(proince[0]+"_"+proince[1]);
			ezTree.setText(proince[1]+"");
			ezTree.setPid(proince[0]+"");
			treeListOfProvince.add(ezTree);
			ProUtilLogger.getRootLogger().info(ezTree.getId());
		}
		
		//create partA node
		List<EzTree> treeListOfPartA=new ArrayList<>();
		String hqlOfPartA="select p from PoPartA p  order by p.city";
		List<PoPartA> partAList=partADao.find(hqlOfPartA);
		for(PoPartA partA:partAList){
			EzTree partANode=new EzTree();
			partANode.setPid(partA.getType()+"_"+partA.getProvince());
			partANode.setText(partA.getName());
			partANode.setId(partA.getId()+"");
			if(idList.contains(partA.getId())){
				partANode.setChecked(true);
			}
			
			Map<String,Object> attrMap=partANode.getAttributes();
			attrMap.put("id",partA.getId()+"");
			attrMap.put("type",partA.getType());
			attrMap.put("degree",partA.getDegree());
			attrMap.put("province",partA.getProvince());
			attrMap.put("name",partA.getName());
			attrMap.put("isLeaf", true);
			System.out.println(partANode.getId());
			treeListOfPartA.add(partANode);
		}
		
		treeList.addAll(treeListOfType);
		treeList.addAll(treeListOfProvince);
		treeList.addAll(treeListOfPartA);
		
		UtilEzTree.setTreeCheckedByIds(treeList, idsChecked);
		UtilEzTree.setTreeLeafStateOpen(treeList);
		
		
		return treeList;
	}
	
	public List<EzTree> getLtrdTreeOfContact(String checkedContactIds,String partAId) {
		List<EzTree> ltrdTrees=new ArrayList<>();
		List<String> idOfOpenStateList=new ArrayList<>();
		if(!UtilString.isNullOrEmpty(partAId)){
			idOfOpenStateList.add("partA_"+partAId);
		}
		
		List<EzTree> partATree=getLtrdTreeOfPartA("");
		//调整 partA tree 的id,避免和后面的 contact id 冲突
		//设为分支
		for(EzTree tree:partATree){
			String id=tree.getId();
			String pid=tree.getPid();
			
			tree.setId("partA_"+id);
			tree.getAttributes().put("isLeaf", false);
			if(pid !=null ){
				tree.setPid("partA_"+pid);
			}
		}
		
		//增加一个其他目录节点：以保存没有单位的联系人.
		EzTree otherTree=new EzTree("partA_other", "其他", false);
		partATree.add(otherTree);
		UtilEzTree.setTreeState(partATree, false);
		
		//处理contact 节点
		List<EzTree> contactTreeList=new ArrayList<>();
		String hql="from PoContact c order by c.name";
		List<PoContact> contactList=contactDao.find(hql);
		for(PoContact contact:contactList){
			EzTree tree;
			if(contact.getPartA()==null){
				tree=new EzTree(contact.getId()+"", contact.getName(), "partA_other", true);
			}else {
				tree=new EzTree(contact.getId()+"", contact.getName(), "partA_"+contact.getPartA().getId(), true);
			}
			Map<String, String> attrMap;
			try {
				attrMap = UtilBean.getSimpleFieldValueMap(contact);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException("UtilBean.getSimpleFieldValueMap has error. ");
			}
			if(contact.getPartA()!=null){
				attrMap.put("partAName", contact.getPartA().getName());
			}else {
				attrMap.put("partAName", "");
			}
			
			tree.getAttributes().putAll(attrMap);
			contactTreeList.add(tree);
		}
		ltrdTrees.addAll(partATree);
		ltrdTrees.addAll(contactTreeList);
		
		if(!UtilString.isNullOrEmpty(checkedContactIds)){
			String[] checkIdArray=checkedContactIds.split(",");
			List<String> checkIdList=Arrays.asList(checkIdArray);
			idOfOpenStateList.addAll(checkIdList);
		}
		
		UtilEzTree.setTreeLeafStateOpen(ltrdTrees);
		UtilEzTree.setTreeCheckedByIds(ltrdTrees, checkedContactIds);
		UtilEzTree.setTreeNodeOpenCascade(ltrdTrees, idOfOpenStateList);
		return ltrdTrees;
	}
	
	/*public List<Tree> getTreeList(String contactIds, String partAId) throws Exception{
		logger.debug("in contactServiceImpl function");
		System.out.println("in contactServiceImpl function");
		List<Tree> treeList=partAService.getTreeList("");
		//parATree节点  attr.ignored=true
		for(Tree partATree:treeList){
			partATree.getAttributes().put("ignored", true);
		}
		
		//增加一个其他目录节点：以保存没有单位的联系人.
		Tree otherTree=new Tree("other", "其他", true);
		treeList.add(otherTree);
		
		// tree list 加入 contact,为避免id冲突，tree.id="contact_"+t.getId()
		String hql="from Tcontact";
		List<Tcontact> contactList=contactDao.find(hql);
		for(Tcontact t:contactList){
			System.out.println(t);
			Tree tree;
			if(t.getPartA()==null){
				//无单位，父节点为other
				//tree=new Tree("contact_"+t.getId(), t.getName(), "other");
				tree=new Tree(t.getId()+"", t.getName(), "other");
			}else{
				//有partA,父节点为partAId
				//tree=new Tree("contact_"+t.getId(), t.getName(), t.getPartA().getId()+"");
				tree=new Tree(t.getId()+"", t.getName(), t.getPartA().getId()+"");
			}
			Map<String, String> attrMap=UtilsBean.getSimpleFieldValueMap(t);
			if(t.getPartA()!=null){
				attrMap.put("partAName", t.getPartA().getName());
			}else {
				attrMap.put("partAName", "");
			}
			
			tree.getAttributes().putAll(attrMap);
			treeList.add(tree);
		}
		UtilEasyUI.setTreeLeafStateOpen(treeList);
		List<Tree> treeLeafSet=UtilEasyUI.getTreeLeafSet(treeList);
		//处理 contactIds
		String[] contactIdArray=contactIds.split(",");
		List<String> contactIdList=new ArrayList<>();
		for(String id:contactIdArray){
			contactIdList.add(id);
		}
		for(Tree t:treeLeafSet){
			if(contactIdList.contains(t.getId())){
				t.setChecked(true);
			}
		}
		
		//处理partAId
		UtilEasyUI.setTreeNodeOpenCascade(treeList, partAId);
		
		logger.debug("partAId="+partAId);
		logger.debug(treeList);
		
		return treeList;
	}*/
}
